package com.xxjz.sc.servers.util;

import com.mysql.cj.util.StringUtils;
import com.xxjz.sc.config.MinioProperties;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Service
public class MinioClientServersImpl implements IMinioClientServers{

    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProperties minioProperties;

    @Override
    public void uploadFile(byte[] data, String filePath,String bucketName) {
        String bucket = bucketName;
        if(StringUtils.isNullOrEmpty(bucketName)){
            bucket = minioProperties.getBucketName();
        }
        InputStream inputStream = new ByteArrayInputStream(data);
        String path;
        try {
            minioClient.putObject(bucket,filePath,inputStream,new PutObjectOptions(inputStream.available(),-1L));
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void deleteFile(String filePath,String bucketName) {
        String bucket = bucketName;
        if(StringUtils.isNullOrEmpty(bucketName)){
            bucket = minioProperties.getBucketName();
        }
        try {
            minioClient.removeObject(bucket,filePath);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getContextType(String filePath) {
        String suffix = filePath.substring(filePath.lastIndexOf(".") + 1 );
        String type = "application/octet-stream";
        if(suffix.equals("jpg")||suffix.equals("jpeg")||suffix.equals("png")||suffix.equals("bmp")||suffix.equals("gif")){
            type = "image/jpeg";
        }
        return type;
    }

    @Override
    public String getObjectURL(String bucketName, String filePath, Integer expires) {
        String bucket = bucketName;
        if(StringUtils.isNullOrEmpty(bucketName)){
            bucket = minioProperties.getBucketName();
        }
        String result = "";
        try {
            result = minioClient.presignedGetObject(bucket, filePath, expires);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidExpiresRangeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }
        return result;
    }
}
